#include "FWContentManager.h"
#include"FWSurface.h"
#include"FWGame.h"
#include"FWTexture.h"
#include"FWDirectShow.h"


FWContentManager::FWContentManager(FWGame* game)
{
	this->_gameObject = game;			
}

FWContentManager::~FWContentManager()
{
}

FWSurface* FWContentManager::loadSurface(string fileName)
{
	this->_mySurface = new FWSurface();
	HRESULT rs;
	LPDIRECT3DSURFACE9 surface;
	D3DXIMAGE_INFO imageInfo;

	rs = D3DXGetImageInfoFromFile(fileName.c_str(), &imageInfo);
	if (FAILED(rs))
		return NULL;

	rs = this->_gameObject->d3ddev->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
	if (FAILED(rs))
		return NULL;

	rs = D3DXLoadSurfaceFromFile(surface, NULL, NULL, fileName.c_str(), NULL, D3DX_DEFAULT, 0, NULL);
	if (FAILED(rs))
		return NULL;

	_mySurface->setHeight(imageInfo.Height);
	_mySurface->setWidth(imageInfo.Width);
	_mySurface->setSurface(surface);
	return _mySurface;
}

FWTexture* FWContentManager::loadTexture(string fileName)
{
	this->_myTexture = new FWTexture();
	HRESULT rs;
	LPDIRECT3DTEXTURE9 texture;
	D3DXIMAGE_INFO imageInfo;

	rs = D3DXGetImageInfoFromFile(fileName.c_str(), &imageInfo);
	if (FAILED(rs))
		return NULL;

	rs = D3DXCreateTextureFromFileEx(this->_gameObject->d3ddev,
		fileName.c_str(),
		imageInfo.Width,
		imageInfo.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(137, 5, 249),
		&imageInfo,
		NULL,
		&texture);
	if (FAILED(rs))
		return NULL;

	this->_myTexture->setTexture(texture);
	this->_myTexture->setHeight(imageInfo.Height);
	this->_myTexture->setWidth(imageInfo.Width);
	return _myTexture;
}

FWDirectShow* FWContentManager::loadSound(string fileName)
{
	this->_mySound = new FWDirectShow();
	this->_mySound->initialize();
	this->_mySound->load(fileName.c_str());
	return this->_mySound;
}